/**
 * Created by wangxiaoqiang on 2017/9/21.
 */
let app = new Vue({
    el:"#goods_list",
    data: {
        resources:[],
        categoryList: [],
        queryParam:{},
        imgPath:fileServerPath
    },
    mounted:function () {
        //默认加载显示第一页
        this.getPageGoods(1);

        this.getCategoryList();
        //跳转到指定的数据分页
        gotoPage(document.querySelector("#page_val"),this.getPageGoods)
    },
    methods: {

        //根据分类或上下架查询
        query:function(){

            let cat_id = $("#cat_id").val();
            let is_shelve = $("#is_shelve").val();
            this.queryParam.cat_id = cat_id;
            this.queryParam.is_shelve = is_shelve;

            JSON.stringify(this.queryParam) === "{}"?this.getPageGoods(1):this.getPageGoods(1,this.queryParam)

        },
        //获取分类列表
        getCategoryList: function () {
            $.get(rootPath + "/category/all", (res) => {
                if (res.meta.code === 200) {
                    this.categoryList = res.data;
                } else console.log(res.meta.code)
            })
        },
        //首页
        first_page: function () {
            JSON.stringify(this.queryParam) === "{}"? this.getPageGoods(1):this.getPageGoods(1,this.queryParam)
        },
        //尾页
        last_page:function () {
            JSON.stringify(this.queryParam) === "{}"?this.getPageGoods(this.resources.totalPage):this.getPageGoods(this.resources.totalPage,this.queryParam)
        },
        //上一页
        pre_page:function () {
            if(this.resources.currentPage !== 1){
                JSON.stringify(this.queryParam) === "{}"?this.getPageGoods(this.resources.currentPage-1):this.getPageGoods(this.resources.currentPage-1,this.queryParam)
            }
        },
        //下一页
        next_page:function () {
            if(this.resources.currentPage !== this.resources.totalPage){
                JSON.stringify(this.queryParam) === "{}"?this.getPageGoods(this.resources.currentPage+1):this.getPageGoods(this.resources.currentPage+1,this.queryParam)
            }
        },
        //根据id删除商品（上下架）
        delete_goods:function (event) {
            let conf = confirm("您确定要删除该商品吗？");
            if(conf){
                //获取商品的id
                let id = event.target.getAttribute("id");
                $.ajax({
                    url:rootPath+"/goods/"+id,
                    type:"DELETE",
                    success: (res)=> {
                        if(res.meta.code === 200){
                            alert("删除成功");
                            this.first_page();
                        }else  alert("删除失败--"+res.meta.code)
                    },
                    error:function (res) {
                        alert("删除失败--"+res.meta.code)
                    }
                });
            }
        },
        /**
         * 修改商品上下架状态
         * @param event
         */
        shelve:function (event) {
            let goods_id = event.target.getAttribute("id");
            let is_shelve =  event.target.getAttribute("shelve-type");
            $.post(rootPath+"/goods/shelve/"+goods_id,{is_shelve:is_shelve},(res)=> {
                if(res.meta.code === 200){
                    alert("修改状态成功");
                    window.location.reload();
                }else alert("修改状态失败--"+res.meta.code)
            });
        },

        //分页通用请求方法
        getPageGoods:function () {
            if(arguments.length === 1){
                $.get(rootPath+"/goods?currentPage="+arguments[0]+"&pageSize="+pageSize,(res)=> {
                    if(res.meta.code === 200){
                        this.resources = res.data;
                    }else this.resources = [];
                    console.log(this.resources)
                })
            }else {
                /*-----根据上下架，分类查询*/
                let str = ``;
                for (let item in arguments[1]){
                    str +=`&`+item+`=`+arguments[1][item];
                }

                $.get(rootPath+"/goods?currentPage="+arguments[0]+"&pageSize="+pageSize+str,(res)=> {
                    if(res.meta.code === 200){
                        this.resources = res.data;
                    }else this.resources = []
                })
            }
        },
        /**
         * 批量修改库存
         */
        edit_num:function () {
            var goods_number_list = $(".goods_number");
            var numobj = {
                stock:[],
                goodsId:[]
            };
            for (var i = 0; i<goods_number_list.length;i++){
                numobj.stock.push($(goods_number_list[i]).val());
                numobj.goodsId.push(goods_number_list[i].dataset.id)
            }
            $.ajax({
                url:rootPath+"/goods/number",
                type:"POST",
                dataType: 'json',
                contentType: 'application/json',
                data: JSON.stringify(numobj),
                success: (res) =>{
                    if(res.meta.code === 200){
                        this.getPageGoods(1);
                    }else{
                        alert(res.meta.message)
                    }
                },
                error:(res) => {
                    console.log(res.meta.message)
                }
            })
        }
    }
});